package chinaums.common.util;

import java.io.IOException;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

/**
 * @author RenLei
 * @ClassName: ShortUrlUtil
 * @version v1.0
 * 2017年7月21日
 */
public class ShortUrlUtil {
	public static String getShortUrl(String long_url) {
		/*try {
			long_url = URLEncoder.encode(long_url, "UTF-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}*/
        String result=getJsonContent2("http://180.149.135.176/2/short_url/shorten.json?source=2849184197&url_long="+long_url);
		return result;
	}
	
	/**
	 *    通过HttpClient解析Json数据，返回结果是一个字符串   @param url   @return 
	 */
	public static String getJsonContent2(String url) {
		String result = "";
		HttpClient httpClient = new DefaultHttpClient();
		HttpGet httpGet = new HttpGet(url);
		HttpResponse response = null;
		try {
			response = httpClient.execute(httpGet);
			if (response.getStatusLine().getStatusCode() == 200) {
				result = EntityUtils.toString(response.getEntity(), "utf-8");
			}
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return result;
	}
	
	public static void main(String[] args) {
		//System.out.println(getShortUrl("https://apply.gzcb.com.cn/index.html?pid=0002&ch1=o2o&ch2=FCXXX"));
	}
}